package com.ipharma.application.services.transaccion;

import java.sql.Timestamp;
import java.util.Iterator;

import com.ipharma.McdError;
import com.ipharma.McdPrs;
import com.ipharma.config.Config;
import com.ipharma.config.FormateadorDeFechas;
import com.ipharma.config.Session;
import com.ipharma.dto.Product;
import com.ipharma.model.Transaccion;

/**
 * A TransactionReposition class.
 * <P>
 * @author JorgE.H
 */
public class TransactionRecharge extends Transaction
{
  /**
   * Attributes
   */

  /**
   * Constructor
   */
  public TransactionRecharge( Session pSess )
  {
    super( pSess );
    type = RECHARGE;
  }

  /**
   * Ends the trasaction, optionally prints a ticket
   * @param pAlert an alert code
   * @return true if succedded, false otherwise
   */
  public boolean end( int pAlert )
  {
    // registers a transaction for each product in database
    boolean lb_alerts = false;

    try
    {
      Iterator l_ite = (Iterator) items.elements();
      while ( l_ite.hasNext() )
      {
        // prepares data
        Product l_prod = (Product) l_ite.next();
        int li_pdtId = l_prod.getId();
        int li_qty = l_prod.getQuantity();
        Config config = (Config) theSess.get(McdPrs.CONFIG);
        int li_dprId = Integer.parseInt( config.getDispenser() );
        int li_type = RECHARGE;
        int li_usrId = theSess.getUsuario().getId().intValue();
        String ls_status = STATUS_OK;
        String ls_date = FormateadorDeFechas.getDateFormater().format( date.getTime() );

        // checks alerts for extraction
        int li_alert = pAlert;
        if ( li_alert > 0 )
          lb_alerts = true;
        Integer l_alert = (li_alert < 0)? null : new Integer( li_alert );

        // inserts transaction into database
        Transaccion transaccion = new Transaccion();
        transaccion.setFecha(new Timestamp(date.getTime().getTime()));
        transaccion.setCantidad(new Integer( li_qty ));
        transaccion.setEstado(ls_status);
        
        Long assignOpt = null;
        if (assignOptId != null)
           assignOpt = new Long(assignOptId.intValue());
        Long assign = null;
        if (assignId != null)
           assign = new Long(assignId.intValue());
        Long patient = null;
        if (patientId != null) 
           patient = new Long(patientId.intValue());
        Long alert = null;
        if (l_alert != null)
           alert = new Long(l_alert.intValue());
        dbTrans.insertTransaction(transaccion, alert, assignOpt, assign, patient, 
                                  new Long(li_type), new Long(li_usrId), 
                                  new Long(li_dprId), new Long(li_pdtId));
        
        // optionally prints a ticket
        if ( config.getTicketGeneration().equals( McdPrs.D_YES ) )
          // prints a ticket
          printTicket( l_prod );
      }
      // removes items from list
      items.clear();
    }
    catch ( Exception e )
    {
      McdError.add( e );
    }
    return lb_alerts;
  }

}
